package com.blb.zy_community.controller;

import com.alibaba.fastjson.JSON;
import com.blb.zy_community.entity.ResponseResult;
import com.blb.zy_community.entity.SysDept;
import com.blb.zy_community.entity.SysMenu;
import com.blb.zy_community.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author 二组
 * @since 2023-06-18
 */
@RestController
@CrossOrigin
public class SysDeptController {
    @Autowired
    private ISysDeptService sysDeptService;

    @GetMapping("/dept-tree")
    public ResponseResult<String> getDeptTree(){
        List<SysDept> deptList = sysDeptService.selectChildDeptTreeByParentId(0L);
        return ResponseResult.ok(deptList);
    }

    @GetMapping("/find-dept")
    public ResponseResult<String> getDept(@RequestParam(required = false,defaultValue = "") String deptName,
                                                 @RequestParam(required = false,defaultValue = "") String status){
        List<SysDept> deptList = sysDeptService.FindDept(0L,deptName,status);
        return ResponseResult.ok(deptList);
    }
    @PostMapping("/dept")
    public ResponseResult<String> addDept(@RequestBody SysDept sysDept){
        sysDeptService.save(sysDept);
        return ResponseResult.ok(sysDept);
    }
    @DeleteMapping("/dept/{id}")
    public ResponseResult<String> deleteDept(@PathVariable Long id){
        sysDeptService.removeById(id);
        return ResponseResult.ok(id);
    }
    @PutMapping("/dept-up")
    public ResponseResult<String> updateDept(@RequestBody SysDept sysDept){
        sysDeptService.updateById(sysDept);
        return ResponseResult.ok(sysDept);
    }

    @GetMapping("/deptTree")
    public ResponseResult<SysDept> getDeptTreeRoot(){
        List<SysDept> sysDeptTree = sysDeptService.selectChildDeptTreeByParentId(0L);
        SysDept sysDept = new SysDept();
        sysDept.setDeptId(9999L);
        sysDept.setDeptName("智慧社区");
        sysDept .setChildDept(sysDeptTree);
        ArrayList<SysDept> sysDeptTree2 = new ArrayList<>();
        sysDeptTree2.add(sysDept);
        String jsonString = JSON.toJSONString(sysDeptTree2);
        System.out.println("-----------------------------------");
        System.out.println(jsonString);
        return ResponseResult.ok(sysDeptTree2);
    }
    @GetMapping("/find-dept2")
    public ResponseResult<String> getDept2(@RequestParam(required = false,defaultValue = "") String deptName,
                                          @RequestParam(required = false,defaultValue = "") String status){

        List<SysDept> sysDepts = sysDeptService.selectChildDeptTreeByParentId(0L);
        if ("".equals(deptName)||deptName==null){
            return ResponseResult.ok(sysDepts);
        }

        List<SysDept> sysDepts2 = sysDeptService.filterByDeptAndStatus(sysDepts,deptName,status);
        return ResponseResult.ok(sysDepts2);
    }

    /**
     * 拿到所有的部门（List）
     * @return
     */
    @GetMapping("/sysDept/list")
    public ResponseResult<SysDept> getSysDeptList(){
        List<SysDept> list = sysDeptService.list();
        return ResponseResult.ok(list);
    }



}
